package com.jzo2o.customer.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.CommonException;
import com.jzo2o.common.model.CurrentUserInfo;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.enums.CertificationStatusEnum;
import com.jzo2o.customer.mapper.WorkerCertificationAuditMapper;
import com.jzo2o.customer.mapper.WorkerCertificationMapper;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.domain.WorkerCertificationAudit;
import com.jzo2o.customer.model.dto.WorkerCertificationUpdateDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditAddReqDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.response.WorkerCertificationAuditResDTO;
import com.jzo2o.customer.service.IWorkerCertificationAuditService;
import com.jzo2o.customer.service.IWorkerCertificationService;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import javax.annotation.Resources;
import java.time.LocalDateTime;

/**
 * <p>
 * 服务人员认证信息审批表 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-09-06
 */
@Service
public class WorkerCertificationAuditServiceImpl extends ServiceImpl<WorkerCertificationAuditMapper, WorkerCertificationAudit> implements IWorkerCertificationAuditService {


    @Resource
    private IWorkerCertificationService workerCertificationService;
    @Override
    public PageResult<WorkerCertificationAuditResDTO> findWorkerCertificationAuditPage(WorkerCertificationAuditPageQueryReqDTO dto) {
        if(ObjectUtils.isEmpty(dto)){
            throw new CommonException("查询参数不能为空");
        }
        //判断数据有效性
        return PageHelperUtils.selectPage(dto,
                () -> baseMapper.selectAllWorkerCertificationAudit());
    }

    @Override
    @Transactional
    public void auditOperation(WorkerCertificationAuditResDTO dto,Long auditId) {
        if(ObjectUtils.isEmpty(dto) || ObjectUtils.isEmpty(auditId)){
            throw new CommonException("查询参数不能为空");
        }
        //判断该id是否存在
        if(ObjectUtils.isEmpty(baseMapper.selectById(auditId))){
            throw new CommonException("该审核目标数据不存在");
        }
        //业务处理
        CurrentUserInfo currentUserInfo = UserContext.currentUser();
        this.lambdaUpdate()
                .eq(WorkerCertificationAudit::getId,auditId)
                .set(WorkerCertificationAudit::getAuditStatus,1)
                .set(WorkerCertificationAudit::getCertificationStatus,dto.getCertificationStatus())
                .set(WorkerCertificationAudit::getRejectReason,dto.getRejectReason())
                .set(WorkerCertificationAudit::getUpdateTime, LocalDateTime.now())
                .set(WorkerCertificationAudit::getAuditorId,currentUserInfo.getId())
                .set(WorkerCertificationAudit::getAuditorName,currentUserInfo.getName())
                .set(WorkerCertificationAudit::getAuditTime,LocalDateTime.now())
                .update();
        WorkerCertificationAudit byId = this.getById(auditId);
        //修改服务人员认证申请表中的状态
        workerCertificationService.lambdaUpdate().eq(WorkerCertification::getId, byId.getServeProviderId())
                .set(WorkerCertification::getCertificationStatus, byId.getCertificationStatus())
                .set(WorkerCertification::getCertificationTime, LocalDateTime.now())
                .set(WorkerCertification::getUpdateTime, LocalDateTime.now()).update();

    }
}
